<?php
namespace Content\Controller;
use Think\Controller;
class IndexController extends Controller {

    private $model_name = 'Nav';
    /**
     * @return mixed
     * 只获取一级菜单
     */
    public function get_first_nav(){
        $nav_model = D($this->model_name);
        $map['parent_id'] = array('eq', 0);
        $map['istofront'] = array('eq', 1);
        $list = $nav_model->where($map)->order('sort asc')->select();
        return $list;
    }

    /**
     * @param $parm 这是一个键值对的数组
     *
     */
    public function get_name_by_id($parm){
        $id = $parm['id'];
        $nav_model = D($this->model_name);
        $map['id'] = array('eq', $id);
        $one = $nav_model->where($map)->find();
        return $one['title'];
    }

    public function get_url_by_id($parm){
        $id = $parm['id'];
        $nav_model = D($this->model_name);
        $map['id'] = array('eq', $id);
        $one = $nav_model->where($map)->find();
        return $one['url'];
    }

    public function get_article_list($parm){
        $pid = $parm['pid'];
        $limit = $parm['limit'];
        $model = D('Typearticle');
        $map['type'] = array('eq', $pid);
        $map['status'] = array('eq', 1);
        $list = $model->where($map)->order('sort desc, createtime desc')->limit($limit)->select();
        return $list;
    }

    public function get_first_article_by_pid($parm){
        $pid = $parm['pid'];
        $model = D('Typearticle');
        $map['type'] = array('eq', $pid);
        $map['status'] = array('eq', 1);
        $one = $model->where($map)->order('sort desc, createtime desc')->find();
        return $one;
    }

    public function get_tops_article($parm){
        $limit = $parm['limit'];
        $model = D('Typearticle');
        $map['status'] = array('eq', 1);
        $list = $model->where($map)->order('sort desc, createtime desc')->limit($limit)->select();
        return $list;
    }

    /**
     * @param $parm
     * 查询父亲菜单,默认是2级菜单
     */
    public function get_parent_nav($parm){
        $id = $parm['id'];
        $nav_model = D($this->model_name);
        $map['id'] = array('eq', $id);
        $one = $nav_model->where($map)->find();
        unset($map);
        $map['id'] = array('eq', $one['parent_id']);
        $parent = $nav_model->where($map)->find();
        return $parent['title'];
    }

    public function get_cur_frined_nav($parm){
        $id = $parm['id'];
        $nav_model = D($this->model_name);
        $map['id'] = array('eq', $id);
        $one = $nav_model->where($map)->find();
        unset($map);
        $map['parent_id'] = array('eq', $one['parent_id']);
        $friedn_list = $nav_model->where($map)->order('sort asc')->select();
        return $friedn_list;
    }

    public function get_nav_location($parm){
        $str = '';
        $id = $parm['id'];
        $nav_model = D($this->model_name);
        $map['id'] = array('eq', $id);
        $one = $nav_model->where($map)->find();
        unset($map);
        $map['id'] = array('eq', $one['parent_id']);
        $parent = $nav_model->where($map)->find();
        $str .= '<a href="'.$parent['url'].'">'.$parent['title'].'</a>>';
        $str .= '<a href="'.$one['url'].'">'.$one['title'].'</a>';
        return $str;
    }
}